//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_makevgroup.sqf"
#include "x_setup.sqf"

private ["_numvecs", "_pos", "_vname", "_grp", "_direction", "_crews", "_grpskill", "_n", "_dir", "_vec", "_npos", "_do_points", "_radius", "_the_vecs", "_nnvnum"];
PARAMS_6(_numvecs,_pos,_vname,_grp,_radius,_direction);
_do_points = (count _this > 6);
_the_vecs = [];
_crews = [];
_npos = _pos;

_grpskill = if (_vname isKindOf "StaticWeapon") then {1.0} else {(GVAR(skill_array) select 0) + (random (GVAR(skill_array) select 1))};

_the_vecs resize _numvecs;
_nnvnum = _numvecs - 1;
for "_n" from 0 to _nnvnum do {
	_dir = if (_direction != -1.111) then {_direction} else {floor random 360};
	
	_vec_ar = [_npos, _dir, _vname, _grp] call FUNC(spawnVehicle);
	_vec = _vec_ar select 0;
	_crews = [_crews, _vec_ar select 1] call FUNC(arrayPushStack2);
	
	_the_vecs set [_n, _vec];
	if (_n < _nnvnum) then {
		_npos = _vec modelToWorld [0,-12,0];
	};
	
	_is_air_vec = false;
	switch (true) do {
		case (_vec isKindOf "Tank"): {
			if !((toUpper _vname) in GVAR(heli_wreck_lift_types)) then {
				_vec addEventHandler ["killed", {_this call FUNC(handleDeadVec)}];
				__addDead(_vec)
			};
			if (GVAR(domdatabase)) then {
				_vec addMPEventHandler ["MPKilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddTankKillPoints)}}];
			};
			if (GVAR(LockArmored) == 0) then {_vec lock true; _vec setVehicleLock "LOCKED"; _vec lockCargo true;};
		};
		case (_vec isKindOf "Wheeled_APC"): {
			if !((toUpper _vname) in GVAR(heli_wreck_lift_types)) then {
				__addDead(_vec)
				_vec addEventHandler ["killed", {(_this select 0) lockCargo false; (_this select 0) call FUNC(handleDeadVec)}];
			};
			if (GVAR(domdatabase)) then {
				_vec addMPEventHandler ["MPKilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddAPCKillPoints)}}];
			};
			if (GVAR(LockCars) == 0) then {_vec lock 2; _vec setVehicleLock "LOCKED";};
		};
		case (_vec isKindOf "Car"): {
			if !((toUpper _vname) in GVAR(heli_wreck_lift_types)) then {
				__addDead(_vec)
				_vec addEventHandler ["killed", {_this call FUNC(handleDeadVec)}];
			};
			if (GVAR(domdatabase)) then {
				_vec addMPEventHandler ["MPKilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddCarKillPoints)}}];
			};
			if (GVAR(LockCars) == 0) then {_vec lock 2; _vec setVehicleLock "LOCKED";};
		};
		case (_vec isKindOf "StaticWeapon"): {
			_vec addEventHandler ["killed", {(_this select 0) lockCargo false; _this call FUNC(handleDeadVec)}];
			__addDead(_vec)
			if (GVAR(domdatabase)) then {
				_vec addMPEventHandler ["MPKilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddCarKillPoints)}}];
			};
			if (GVAR(LockStatic) == 0) then {_vec lock 2; _vec setVehicleLock "LOCKED"; _vec lockCargo true;};
		};
		default {
			if !((toUpper _vname) in GVAR(heli_wreck_lift_types)) then {
				__addDead(_vec)
			};
			if (GVAR(domdatabase)) then {
				if (_vec isKindOf "Helicopter") then {
					_vec addMPEventHandler ["MPKilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddChopperKillPoints)}}];
				} else {
					if (_vec isKindOf "Plane") then {
						_vec addMPEventHandler ["MPKilled", {if (isServer && {isPlayer (_this select 1)}) then {(_this select 1) call FUNC(PAddPlaneKillPoints)}}];
					};
				};
			};
			if (_vec isKindOf "Air") then {_is_air_vec = true;if (GVAR(LockAir) == 0) then {_vec lock 3}};
		};
	};
	if (GVAR(LockCargo) == 0) then {_vec lockCargo true;};
	#ifdef __TT__
	if (_do_points) then {
		if (!_is_air_vec) then {
			_vec addMPEventHandler ["MPkilled", {if (isServer) then {[[20, 3, 2, 1],_this select 1, _this select 0] call FUNC(AddKills);(_this select 0) removeAllMPEventHandlers "MPKilled"}}];
		} else {
			_vec addMPEventHandler ["MPkilled", {if (isServer) then {[[30, 3, 2, 1],_this select 1, _this select 0] call FUNC(AddKills);(_this select 0) removeAllMPEventHandlers "MPKilled"}}];
		};
	};
	#endif
	if (GVAR(with_ai) && {GVAR(with_ranked)}) then {_vec addMPEventHandler ["MPkilled", {if (isServer) then {[5,_this select 1] call FUNC(AddKillsAI);(_this select 0) removeAllMPEventHandlers "MPKilled"}}]};
	// TYRGHEN - Adds some built in scripted event handlers to units to make them more reactive
	if (!(isNil QGVAR(with_aihandlers)) && {GVAR(with_aihandlers) == 0}) then {
		_crews call FUNC(scriptedreactions);
		_the_vecs call FUNC(scriptedvecreactions);
	};
};
(leader _grp) setSkill _grpskill;
[_grp, 1] call FUNC(setGState);
[_the_vecs, _crews]